﻿using System.Collections.Generic;
using System.Linq;
using Magiq.Objects;

namespace Magiq {
  public static class Magiq {
    private static readonly IList<IMagiqProvider> factories = new List<IMagiqProvider>();

    static Magiq() {
      Register<MagiqToObjects>();
    }

    public static IMagiqConfigurator Register<T>() where T : IMagiqProvider, new() {
      return new Configurator().Register<T>();
    }

    public static IMagiqConfigurator Register(IMagiqProvider magiqToSomething) {
      return new Configurator().Register(magiqToSomething);
    }

    public static IMagiqProvider For(IQueryable queryable) {
      return factories.First(x => x.Handles(queryable));
    }

    public static IMagiqConfigurator Clear() {
      factories.Clear();
      return Register<MagiqToObjects>();
    }

    #region Nested type: Configurator

    private class Configurator : IMagiqConfigurator {
      #region IMagiqConfigurator Members

      public IMagiqConfigurator Register<T>() where T : IMagiqProvider, new() {
        return Register(new T());
      }

      public IMagiqConfigurator Register(IMagiqProvider magiqToSomething) {
        factories.Insert(0, magiqToSomething);
        return this;
      }

      #endregion
    }

    #endregion
  }
}